package com.ts.content.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ts.content.dto.PostDTO;
import com.ts.content.entity.PostEntity;
import com.ts.content.entity.TopicEntity;
import com.ts.content.entity.UserEntity;
import com.ts.content.mapper.PostMapper;
import com.ts.content.service.*;
import com.ts.content.vo.Post2Vo;
import com.ts.content.vo.PostTopicVo;
import com.ts.content.vo.PostVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 帖子
 *
 * @author admin admin@qq.com
 * @since 1.0.0 2023-02-22
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, PostEntity> implements PostService {

    @Autowired
    private UserService userService;

    @Autowired
    private TopicService topicService;

    @Autowired
    private FollowService followService;

    @Autowired
    private LikeService likeService;

    @Override
    public IPage getPostList(IPage<PostVo> page) {
        return baseMapper.getPostList(page);
    }

    @Override
    public void postSave(PostDTO postEntity) {
        baseMapper.postSave(postEntity.getUserId(),postEntity.getClassId(),postEntity.getTitle(),postEntity.getContent(),postEntity.getMedia(),postEntity.getDiscussList(),postEntity.getCreateTime());
    }

    @Override
    public List<Post2Vo> getPostListByjoinTopic(Long page, Integer userId, Integer order) {

        //查询帖子详情
        Page page1 = new Page();
        page1.setCurrent(page);
        Page<PostEntity> postListByjoinTopic = baseMapper.getPostListByjoinTopic(page1, userId,order);

        //将page转换成list
        List<PostEntity> records = postListByjoinTopic.getRecords();
        List<Post2Vo> post2VoList = new ArrayList<>();
        //遍历集合
        for (PostEntity postEntity : records) {
            if (ObjectUtils.isEmpty(postEntity)){
                return post2VoList;
            }
            //新建一个vo,将遍历的数据放入vo中
            Post2Vo post2Vo = new Post2Vo();
            BeanUtils.copyProperties(postEntity,post2Vo);
            String media = postEntity.getMedia();
            String discussList = postEntity.getDiscussList();
            //不为空时拆分放入集合中
            if (!ObjectUtils.isEmpty(media)){
                post2Vo.setMedia(Arrays.asList(media.split(",")));
            }else {
                post2Vo.setMedia(Collections.emptyList());
            }

            if (!ObjectUtils.isEmpty(discussList)){
                post2Vo.setDiscussList(Arrays.asList(discussList.split(",")));
            }else {
                post2Vo.setDiscussList(Collections.emptyList());
            }
            post2Vo.setThumbNum(Math.toIntExact(likeService.likeCount(1, postEntity.getId())));
            //查询当前用户是否对帖子点赞了
            Boolean like = likeService.isLike(1, postEntity.getId(), userId);
            post2Vo.setIsThumb(like);

            //将用户放入vo中的UserInfo中
            post2Vo.setUserInfo(userService.getById(postEntity.getUserId()));
            post2VoList.add(post2Vo);
        }

        return  post2VoList;
    }

    //根据帖子id查询帖子详情，包括个人信息
    @Override
    public PostTopicVo getPostTopicById(Integer userId,Integer postId) {
        //查询帖子详情
        PostEntity postEntity = baseMapper.selectById(postId);

        //根据用户id查询用户信息
        UserEntity userInfo = userService.getById(postEntity.getUserId());
        userInfo.setPhone(null);
        userInfo.setBirthday(null);
        userInfo.setStatus(null);
        userInfo.setOpenid(null);

        //根据圈子id查询圈子详情
        TopicEntity topicEntity = topicService.getById(postEntity.getTopicId());
        PostTopicVo postTopicVo = new PostTopicVo();

        //将原实体类拷贝到vo中
        BeanUtils.copyProperties(postEntity,postTopicVo);
        String media = postEntity.getMedia();
        String discussList = postEntity.getDiscussList();
        if (!ObjectUtils.isEmpty(media)){
            postTopicVo.setMedia(Arrays.asList(media.split(",")));
        }else {
            postTopicVo.setMedia(Collections.emptyList());
        }

        if (!ObjectUtils.isEmpty(discussList)){
            postTopicVo.setDiscussList(Arrays.asList(discussList.split(",")));
        }else {
            postTopicVo.setDiscussList(Collections.emptyList());
        }

        //把用户信息和圈子信息放入vo中
        postTopicVo.setTopicInfo(topicEntity);
        postTopicVo.setUserInfo(userInfo);
        //查询当前用户是否关注
        postTopicVo.setIsFollow(followService.isFollow(userId,postEntity.getUserId()));

        return postTopicVo;
    }
}